package com.matriq.cloudmanager.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public abstract class DownloadRecevicer extends BroadcastReceiver {
	public final static int ACTION_PROGRESS_CHANGE = 1;
	public final static int ACTION_FINISH = 2;
	public final static int ACTION_ERROR = 3;
	public final static int ACTION_CANCEL =4;

	@Override
	public void onReceive(Context context, final Intent intent) {
		int action = intent.getIntExtra("action", -1);
		switch (action) {
		case ACTION_PROGRESS_CHANGE:
			String url = intent.getStringExtra("url");
			long position = intent.getLongExtra("position", -1);
			long length = intent.getLongExtra("length", -1);
			long itemId = intent.getLongExtra("itemId", -1);
			onProgressChange(itemId, url, position, length);
			break;
		case ACTION_FINISH:
			new Runnable() {
				public void run() {
					String url = intent.getStringExtra("url");
					String path = intent.getStringExtra("path");
					long itemId = intent.getLongExtra("itemId", -1);
					onFinish(itemId, url, path);
				}
			}.run();
			break;
		case ACTION_ERROR:
			new Runnable() {
				public void run() {
					String url = intent.getStringExtra("url");
					long itemId = intent.getLongExtra("itemId", -1);
					onError(itemId, url);
				}
			}.run();
			break;
		case ACTION_CANCEL:
			new Runnable() {
				public void run() {
					String url = intent.getStringExtra("url");
					long itemId = intent.getLongExtra("itemId", -1);
					onCancel(itemId, url);
				}
			}.run();
			break;

		default:
			break;
		}
	}

	public abstract void onProgressChange(long itemId, String url, long position, long length);

	public abstract void onFinish(long itemId, String url, String path);

	public abstract void onError(long itemId, String url);
	public abstract void onCancel(long itemId, String url);

	public static Intent getProgressChangeIntent(long itemId, String url, long position, long length) {
		Intent it = new Intent("net.lgpage.service.download");
		it.putExtra("action", ACTION_PROGRESS_CHANGE);
		it.putExtra("itemId", itemId);
		it.putExtra("url", url);
		it.putExtra("position", position);
		it.putExtra("length", length);
		return it;
	}

	public static Intent getErrorIntent(long itemId, String url) {
		Intent it = new Intent("net.lgpage.service.download");
		it.putExtra("itemId", itemId);
		it.putExtra("url", url);
		it.putExtra("action", ACTION_ERROR);
		return it;
	}
	public static Intent getCancelIntent(long itemId, String url) {
		Intent it = new Intent("net.lgpage.service.download");
		it.putExtra("itemId", itemId);
		it.putExtra("url", url);
		it.putExtra("action", ACTION_CANCEL);
		return it;
	}

	public static Intent getFinishIntent(long itemId, String url, String path) {
		Intent it = new Intent("net.lgpage.service.download");
		it.putExtra("itemId", itemId);
		it.putExtra("url", url);
		it.putExtra("path", path);
		it.putExtra("action", ACTION_FINISH);
		return it;
	}
}
